package ModelImplementation

import (
	"PaySalary/Model"
	"math"
	"time"
)

type HourlyClassification struct {
	itsRate float64
	itsTimeCards map[time.Time]TimeCard
}

func hourlyClassification(hourlyRate float64) HourlyClassification{
	return HourlyClassification{itsRate: hourlyRate}
}

func (hc* HourlyClassification)GetRate() float64 {
	return hc.itsRate
}

func GetTimeCard(date time.Time) TimeCard {
	return TimeCard{itsDate: date}
}

func (hc* HourlyClassification) AddTimeCard (tc TimeCard) {
	hc.itsTimeCards[tc.GetDate()] = tc
}

func (hc* HourlyClassification)CalculatePayForTimeCard(tc TimeCard) float64 {
	hours := tc.GetHours()
	overtime := math.Max(0.0,hours - 8.0)
	straightTime := hours - overtime
	return straightTime * hc.itsRate + overtime * hc.itsRate * 1.5
}

func (hc* HourlyClassification) CalculatePay(pc Model.Paycheck) float64 {
	totalPay := 0.0
	//payPeriodEndDate := pc.GetPayPeriodEndDate()
	for date:= range hc.itsTimeCards {
		tc := hc.itsTimeCards[date]

		dt1 := pc.GetPayPeriodStartDate()
		dt2 := pc.GetPayPeriodEndDate()

		if date.After(dt1) && date.Before(dt2) {
			totalPay += hc.CalculatePayForTimeCard(tc)
		}
	}
	return totalPay
}

